// pages/courttracks/all/index.js
const app = getApp()
import api from '../../../api/main.js'
import tools from '../../../utils/tools.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 1,
    page: 1,
    pageSize: 10,
    trackList: [],
    isRefresh: true,
    isMoreHid: false,
    isData: true,
    total: 0,
    isPermissions: false,
    noDataMessage: '暂时还没有任何已发布的动态哦~',
    currentTabTitle: '已发布的动态'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.loadData()
  },
  /**
   * 加载数据
   */
  loadData() {
    var that = this
    if (that.data.page == 1) {
      that.setData({
        isRefresh: true,
        caseList: []
      })
    } else {
      that.setData({
        isMoreHid: true
      })
    }
    api.track.getCourtTrackList(that.data.currentTab, that.data.page, that.data.pageSize).then(res => {
      var isData = false
      if (res.code == app.globalData.successCode && res.success) {
        const total = 0
        var array = res.data.list
        if (array.length > 0) {
          isData = true
        }
        const permissions = res.data.permissions
        if (permissions && permissions.indexOf('dynamicsCheck') > -1) {
          that.data.isPermissions = true
        }
        var trackArray = []
        for (var i = 0; i < array.length; i++) {
          var item = array[i]
          if (item.insertTime) {
            item['insertTime'] = tools.stringToDate(parseInt(item.insertTime), 'Y-M-D h:m:s')
          } else {
            item['insertTime'] = ''
          }
          if (item.thumb && item.thumb !=''){
            item['thumb'] = app.resourcesFullUrl(item.thumb)
          }else{
            item['thumb'] = ''
          }
          trackArray.push(item)
        }
        if (that.data.page == 1) {
          that.setData({
            isRefresh: false,
            isData: isData,
            trackList: trackArray,
            isPermissions: that.data.isPermissions,
            total: total
          })
        } else {
          that.setData({
            isMoreHid: false,
            isData: isData,
            trackList: array,
            permissions: res.data.permissions,
            total: total
          })
        }
        array = null
        trackArray = null
      }
      res = null
    })
  },
  /**
   * 点击tab切换
   */
  swichNav: function(e) {
    var message = '暂时还没有任何已发布的动态哦'
    var title = '已发布的动态'
    const index = parseInt(e.target.dataset.current)
    switch (index) {
      case 0:
        message = '暂时还没有任何待审核的动态哦~'
        title = '待审核的动态'
        break
      case -1:
        message = '暂时还没有任何被驳回的动态哦~'
        title = '被驳回的动态'
        break
      case 2:
        message = '暂时还没有任何已关闭的动态哦~'
        title = '已关闭的动态'
        break
    }
    var that = this
    if (that.data.currentTab === e.target.dataset.current) {
      return
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        page: 1,
        isData: true,
        noDataMessage: message,
        currentTabTitle: title
      })
      that.loadData()
    }
  },
  /**
   * 改变动态的这套
   * @param index 1 审核 2 关闭 0 恢复 -1 驳回 99 删除
   * @param id 数据ID
   */
  onModifyStatus(e) {
    var that = this
    var message = '您确定要驳回该动态吗？'
    const index = parseInt(e.target.dataset.index)
    const id = e.target.dataset.id
    switch (index) {
      case 0:
        message = '您确定要恢复该动态吗？'
        break
      case 1:
        message = '您确定要审核该动态吗？'
        break
      case 2:
        message = '您确定要关闭该动态吗？'
        break
      case 99:
        message = '您确定要删除该动态吗？'
        break
    }
    wx.showModal({
      title: '温馨提示',
      content: message,
      success(res) {
        if (res.confirm) {
          that.modifyStatus(id, index)
        }
      }
    })
  },
  /**
   * 保存动态状态
   * @param status 1.审核通过，0.待审核，-1驳回，2关闭
   * @param id 数据ID
   */
  modifyStatus(id, status) {
    wx.showLoading({
      title: '正在操作...',
    })
    if (status == 99) {
      api.track.deleteTrack(id).then(res => {
        wx.hideLoading()
        if (res.code == app.globalData.successCode && res.success) {
          this.loadData()
        } else {
          wx.showToast({
            title: res.msg,
            icon: ''
          })
        }
      })
    } else {
      api.track.modifyStatus(id, status).then(res => {
        wx.hideLoading()
        if (res.code == app.globalData.successCode && res.success) {
          this.loadData()
        } else {
          wx.showToast({
            title: res.msg,
            icon: ''
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})